
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.PetBreeds;
import me.zhengjie.modules.system.domain.PetCategories;
import me.zhengjie.modules.system.domain.vo.PetBreedsQueryCriteria;
import me.zhengjie.modules.system.service.PetBreedsService;
import me.zhengjie.utils.ColUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.PetCategoriesService;
import me.zhengjie.modules.system.domain.vo.PetCategoriesQueryCriteria;
import me.zhengjie.modules.system.mapper.PetCategoriesMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author ck
* @date 2025-02-11
**/
@Service
@RequiredArgsConstructor
public class PetCategoriesServiceImpl extends ServiceImpl<PetCategoriesMapper, PetCategories> implements PetCategoriesService {

    private final PetCategoriesMapper petCategoriesMapper;

    private final PetBreedsService petBreedsService;

    @Override
    public PageResult<PetCategories> queryAll(PetCategoriesQueryCriteria criteria, Page<Object> page){
        PageResult<PetCategories> categoriesPageResult = PageUtil.toPage(petCategoriesMapper.findAll(criteria, page));
        List<PetCategories> categoriesList = categoriesPageResult.getContent();
        for (PetCategories categories : categoriesList) {
            Integer id = categories.getId();
            PetBreedsQueryCriteria petBreedsQueryCriteria = new PetBreedsQueryCriteria();
            petBreedsQueryCriteria.setCategoryId(id);
            List<PetBreeds> petBreeds = petBreedsService.queryAll(petBreedsQueryCriteria);
            categories.setHasChildren(CollectionUtils.isNotEmpty(petBreeds));
        }
        return categoriesPageResult;
    }

    @Override
    public List<PetCategories> queryAll(PetCategoriesQueryCriteria criteria){
        return petCategoriesMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PetCategories resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PetCategories resources) {
        PetCategories petCategories = getById(resources.getId());
        petCategories.copy(resources);
        saveOrUpdate(petCategories);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<PetCategories> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PetCategories petCategories : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("类别名称", petCategories.getName());
            map.put("类别图片URL", petCategories.getImageUrl());
            map.put("类别描述", petCategories.getDescription());
            map.put("平均寿命", petCategories.getAverageLifeSpan());
            map.put("护理频率编码", petCategories.getGroomingFrequencyCode());
            map.put("常见健康问题", petCategories.getCommonHealthIssues());
            map.put("排序号", petCategories.getSortOrder());
            map.put("是否启用 0-禁用 1-启用", petCategories.getIsActive());
            map.put("创建时间", petCategories.getCreatedAt());
            map.put("更新时间", petCategories.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}